<?php
namespace application\index\model;
use think\Db;

class ArticleFront
{
	public $code;
	public $message;

	/**
	 * 首页数据，集中用缓存处理
	 */
	public function indexData()
	{
		//是否存在缓存
		$cache = md5(__METHOD__);
		if(safeCache($cache)) return safeCache($cache);
		$res['rollBar']	= $this->getRollbar();	//滚动条
		$res['promote'] = $this->getPromote();	//推荐文章
		$res['newest'] 	= $this->getNewestArtilce(1, 15);	//最新新闻
		$res['newstag'] = $this->getNewstag();	//新闻热门标签
		$res['hotest'] 	= $this->getHotest();	//热门文章
		$res['count']  	= $this->getCount();	//站点统计
		$res['ads']	   	= $this->getAds();	//广告
		$res['link']	= (new \application\common\model\SiteLink())->getIndexList();//友情链接
		safeCache($cache, $res);
		return $res;
	}
	/**
	 * 列表页数据
	 * @param int $catid 分类id
	 */
	public function catData($catid, $page = 1, $psize = 10)
	{
		//是否存在缓存
		$cache = md5(__METHOD__.implode(',', func_get_args()));
		if(safeCache($cache)) return safeCache($cache);
		$res['catinfo']	= $this->getCatInfo($catid);	//分类信息
		$res['list'] 	= $this->getCatNewsList($catid, $page, $psize);//文章列表
		$res['yunlist'] = $this->getSiteYun();	//资讯云
		$res['hotest'] 	= $this->getCatHotNews($catid);	//热门文章
		$res['page']	= $this->getCatPage($catid, $page);	//文章分页
		safeCache($cache, $res);
		return $res;
	}
	/**
	 * 查看评论
	 * @param int $catid 分类id
	 */
	public function commentData($aid, $page = 1, $psize = 25)
	{
		//是否存在缓存
		$cache = md5(__METHOD__.implode(',', func_get_args()));
		if(safeCache($cache)) return safeCache($cache);
		$res['article'] = Db::table('article_base')->field(['title'])->where(['aid' => $aid])->find();
		$res['reply']   = $this->getArticleReply($aid, $page, $psize);	//回复评论
		$res['page']	= $this->getCommentPage($aid, $page, $psize);	//评论分页
		safeCache($cache, $res);
		return $res;
	}
	/**
	 * 内容详情
	 * @param $aid 文章id
	 */
	public function detail($aid)
	{
		$cache = md5(__METHOD__.implode(',', func_get_args()));
		if(safeCache($cache)) return safeCache($cache);
		$res['article'] = $this->getArticleDetail($aid);	//内容详情
		$res['reply']	= $this->getArticleReply($aid);	//回复评论
		$res['promote'] = $this->getRelateArticle($aid, 6);	//推荐文章
		$res['hotest'] 	= $this->getHotest();	//热门文章
		if(!$res['article']) return false;
		safeCache($cache, $res);
		return $res;
	}
	/**
	 * 文章评论
	 */
	public function reply()
	{
		$aid = input('aid');
		$replier = input('comment_user');
		$reply_cnt = input('comment_cnt');
		if(!$aid || !$replier || !$reply_cnt) {
			$this->code = '1500';
			$this->message = '评论参数错误';
			return false;
		}
		//保存认证
		$data = [
			'aid' 		 => $aid,
			'replier' 	 => $replier,
			'reply_cnt'  => $reply_cnt,
			'reply_time' => time()
		];
		if(Db::table('article_reply')->insert($data)) {
			return true;
		} else {
			$this->code = '1000';
			$this->message = '系统出错，请稍候再试';
			return false;
		}
	}
	/**
	 * 广告
	 */
	private function getAds()
	{
		$data = Db::table('ads')->where(['position' => 'listbottom', 'status' => 1, 'expire_time' => ['>', time()]])->order('orderlist DESC')->select();
		return $data;
	}
	/**
	 * 内容详情
	 */
	private function getArticleDetail($aid)
	{
		$sql = 'SELECT a.*,
				b.summary,b.content,b.taglist,
				c.catid,c.cat_name
				FROM article_base AS a 
				LEFT JOIN article_detail AS b ON b.aid=a.aid 
				LEFT JOIN article_cat AS c ON c.catid=a.catid 
				WHERE a.status=1 AND a.aid=%d';
		$res = Db::query(sprintf($sql, $aid));
		if($res && isset($res[0])) {
			return $res[0];
		} else {
			return false;
		}
	}
	/**
	 * 回复评论
	 */
	private function getArticleReply($aid, $page=1, $psize=20)
	{
		return Db::table('article_reply')
				->where(['aid' => $aid])
				->order('reply_id DESC')
				->page($page, $psize)
				->select();
	}
	/**
	 * 分类信息
	 * @param int $catid 分类id
	 */
	private function getCatInfo($catid)
	{
		$res = Db::table('article_cat')
				->where(['catid' => $catid])
				->find();
		return $res;
	}
	/**
	 * 分类文章列表
	 */
	private function getCatNewsList($catid, $page = 1, $psize = 10)
	{
		$sql = 'SELECT a.aid,a.title,a.headpic,a.reply_count,a.pub_time,
				b.summary,
				c.catid,c.cat_name
				FROM article_base AS a 
				LEFT JOIN article_detail AS b ON b.aid=a.aid 
				LEFT JOIN article_cat AS c ON c.catid=a.catid 
				WHERE a.status=1 AND a.catid=%d
				ORDER BY a.aid DESC 
				LIMIT %d,%d';
		$res = Db::query(sprintf($sql, $catid, ($page-1)*$psize, $psize));
		return $res;
	}
	/**
	 * 分类热门文章
	 */
	private function getCatHotNews($catid, $page = 1, $psize = 10)
	{
		$sql = 'SELECT a.aid,a.title,a.headpic,a.reply_count,a.pub_time,
				b.summary,
				c.catid,c.cat_name
				FROM article_base AS a 
				LEFT JOIN article_detail AS b ON b.aid=a.aid 
				LEFT JOIN article_cat AS c ON c.catid=a.catid 
				WHERE a.status=1 AND a.catid=%d AND (a.pub_time BETWEEN %d AND %d) 
				ORDER BY a.reply_count DESC 
				LIMIT %d,%d';
		$res = Db::query(sprintf($sql, $catid, time()-2592000, time(), ($page-1)*$psize, $psize));
		return $res;
	}
	/**
	 * 分类文章分页
	 */
	private function getCatPage($catid, $page = 1, $psize = 10)
	{
		$_GET['page'] = $page;
		$count = Db::table('article_base')->where(['catid' => $catid])->count();
		$page  = new \application\common\model\Page($count, $psize, ['url' => '/cat/'.$catid.'/']);
		$res = $page->show(true);
		return $res;
	}
	/**
	 * 评论分页
	 */
	private function getCommentPage($aid, $page = 1, $psize = 25)
	{
		$_GET['page'] = $page;
		$count = Db::table('article_reply')->where(['aid' => $aid])->count();
		$page  = new \application\common\model\Page($count, $psize, ['url' => '/comment/'.$aid.'/']);
		$res = $page->show(true);
		return $res;
	}
	/**
	 * 站点统计
	 */
	private function getCount()
	{
		$data = [
			'num'  	=> Db::table('article_base')->count(),
			'date' 	=> ceil( (time()-1499644800)/86400 ),
			'email' => 'jbk3311@qq.com'
		];
		return $data;
	}
	/**
	 * 最热门
	 */
	private function getHotest($page = 1, $psize = 10)
	{
		$sql = 'SELECT a.aid,a.title,a.headpic,a.reply_count,a.pub_time,
				b.summary,
				c.cat_name
				FROM article_base AS a 
				LEFT JOIN article_detail AS b ON b.aid=a.aid 
				LEFT JOIN article_cat AS c ON c.catid=a.catid 
				WHERE a.status=1 AND a.pub_time BETWEEN %d AND %d 
				ORDER BY a.reply_count DESC 
				LIMIT %d,%d';
		$res = Db::query(sprintf($sql, time()-2592000, time(), ($page-1)*$psize, $psize));
		return $res;
	}
	/**
	 * 最新发布
	 */
	private function getNewestArtilce($page = 1, $psize = 10)
	{
		$sql = 'SELECT a.aid,a.title,a.headpic,a.reply_count,a.pub_time,a.catid,
				b.summary,
				c.cat_name
				FROM article_base AS a 
				LEFT JOIN article_detail AS b ON b.aid=a.aid 
				LEFT JOIN article_cat AS c ON c.catid=a.catid 
				WHERE a.status=1 
				ORDER BY a.aid DESC 
				LIMIT %d,%d';
		$res = Db::query(sprintf($sql, ($page-1)*$psize, $psize));
		return $res;
	}
	/**
	 * 热门新闻标签
	 */
	private function getNewstag()
	{
		$data = [
			[
				'name' => '凤凰头条',
				'url'	=> '/cat/id/1'
			]
		];
		return $data;
	}
	/**
	 * 推荐文章(取3条)
	 */
	private function getPromote($num = 3)
	{
		$sql = 'SELECT a.aid,a.title,a.headpic,a.reply_count,a.pub_time,
				b.summary,
				c.cat_name
				FROM article_promote AS p 
				LEFT JOIN article_base AS a ON a.aid=p.aid 
				LEFT JOIN article_detail AS b ON b.aid=a.aid 
				LEFT JOIN article_cat AS c ON c.catid=a.catid 
				WHERE p.status=1 
				ORDER BY p.listorder DESC 
				LIMIT %d';
		$res = Db::query(sprintf($sql, $num));
		return $res;
	}
	/**
	 * 获取文章相似推荐列表
	 * @param int $aid
	 */
	private function getRelateArticle($aid, $num = 3)
	{
		$sql = 'SELECT a.aid,a.title,a.headpic,a.reply_count,a.pub_time,
				b.summary,
				c.cat_name
				FROM article_base AS a 
				LEFT JOIN article_detail AS b ON b.aid=a.aid 
				LEFT JOIN article_cat AS c ON c.catid=a.catid 
				WHERE a.status=1 AND a.aid<%d AND a.catid=(SELECT catid FROM article_base WHERE aid=%d) 
				ORDER BY a.aid DESC 
				LIMIT %d';
		$res = Db::query(sprintf($sql, $aid, $aid, $num));
		return $res;
	}
	/**
	 * 滚动条
	 */
	private function getRollbar()
	{
		$data = Db::table('ads')->where(['position' => 'indexbar', 'status' => 1, 'expire_time' => ['>', time()]])->order('orderlist DESC')->select();
		return $data;
	}
	/**
	 * 资讯云
	 */
	private function getSiteYun()
	{
		$res = Db::table('site_yun')->where(['status' => 1])->select();
		return $res;
	}
}